<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <!--<TodoHeader @addTodo="addTodo"/>--><!--给todo-header标签对象绑定addTodo事件监听-->
      <TodoHeader ref="header"/>
      <todo-list :todos="todos" />
      <!--<todo-footer :todos="todos" :deleteCompleteTodos="deleteCompleteTodos"
          :selectAllTodos="selectAllTodos"/>-->
      <todo-footer>
        <input type="checkbox" v-model="isAllCheck" slot="checkAll"/>
        <span slot="count">已完成{{completeSize}} / 全部{{todos.length}}</span>
        <button class="btn btn-danger" v-show="completeSize" @click="deleteCompleteTodos" slot="delete">清除已完成任务</button>
      </todo-footer>
    </div>
  </div>
</template>

<!--
绑定事件监听 --订阅消息
触发事件 --发布消息
-->
<script>
  import PubSub from 'pubsub-js'
  import TodoHeader from './components/TodoHeader'
  import TodoList from './components/TodoList'
  import TodoFooter from './components/TodoFooter'
  import storageUtil from './util/storageUtil'

export default {

  data () {
    return {
      //从localStorage读取todos
      //JSON.parse(window.localStorage.getItem('todos_key') || '[]')
      todos: storageUtil.readTodos()
    }
  },

  computed: {
    completeSize () {
      return this.todos.reduce((preTotal, todo) => preTotal + (todo.complete?1:0), 0)
    },
    //计算属性高级
    isAllCheck: {
      //回调函数 当需要读取当前属性值时回调，根据相关的数据计算并返回当前属性的值
      get () {
        return this.completeSize === this.todos.length && this.completeSize > 0
      },
      // 回调函数，监视当前属性值的变化，当属性值发生改变时回调，更新相关的属性数据
      set (value) { //value是当前checkbox最新的值
        this.selectAllTodos(value)
      }
    }

  },

  mounted () {//执行异步代码
    //给<TodoHeader/>绑定addTodo事件监听
    //this.$on('addTodo', this.addTodo) //给App绑定的监听，不对
    //ref : 为某个元素注册一个唯一标识, vue对象通过$refs属性访问这个元素对象
    this.$refs.header.$on('addTodo', this.addTodo)

    //订阅消息
    PubSub.subscribe('deleteTodo', (msg, index) => {
      this.deleteTodo(index)
    })
  },

  methods: {
    addTodo (todo) {
      this.todos.unshift(todo)
    },
    deleteTodo (index) {
      this.todos.splice(index, 1)
    },
    //删除所有选中的todo
    deleteCompleteTodos () {
      this.todos = this.todos.filter(todo => !todo.complete)
    },
    //全选/全不选
    selectAllTodos (check) {
      this.todos.forEach(todo => todo.complete = check)
    }
  },

  watch: { //深度监视
    todos: {
      deep: true, //深度监视
      /*handler: function (value) {
        //将todos最新值的json数据，保存到localStorage
        //window.localStorage.setItem('todos_key', JSON.stringify(value))
        storageUtil.saveTodos(value)
      }*/
      handler: storageUtil.saveTodos
    }
  },

  components: {
    TodoHeader,
    TodoList,
    TodoFooter
  }
}
</script>

<style>
  @import './assets/css/base.css';

  .todo-container {
    width: 600px;
    margin: 0 auto;
  }
  .todo-container .todo-wrap {
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 5px;
  }
</style>
